home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / readiff.e < prev    next >
Text File  |  1995-01-11  |  3KB  |  140 lines

  1.  
  2. /****
  3.  
  4.     ReadIFF.e
  5.     © 1995 by Vidar Hokstad <vidarh@rforum.no>
  6.  
  7.  
  8.     COPYRIGHT NOTICE:
  9.  
  10.     This code can be distributed freely, and parts of the code,
  11.     or the whole code, can be used as is, or reused in any
  12.     product - free or commercial - provided the following terms
  13.     are met:
  14.  
  15.     - You accept that I give no guarantee, expressed or implied
  16.         of the usefulness or functionality of this code, and that
  17.         I accept no responsability for damage caused directly or
  18.         indirectly by the use of this program.
  19.  
  20.     - The product in which the code is used can not be used
  21.         for military applications.
  22.  
  23.  
  24.     INFO:
  25.  
  26.     Written as an exersize in using iffparse.library. It reads an
  27.     IFF file, and writes it's structure to stdout in a simple
  28.     format.
  29.  
  30. ***/
  31.  
  32.  
  33. OPT OSVERSION=37
  34.  
  35. MODULE 'iffparse','libraries/iffparse'
  36.  
  37. RAISE "^C" IF CtrlC() = TRUE
  38.  
  39. PROC readiff (name) HANDLE
  40.  
  41.     DEF iff:PTR TO iffhandle,        -> Utility struct. for iffparse
  42.         node:PTR TO contextnode,    -> additional info about chunk
  43.         fh,                            -> File handle of an IFF file
  44.         nest,                        -> How many levels deep in the
  45.                                     -> file are we?
  46.         ret,count
  47.  
  48. -> --- INIT
  49.  
  50.     IF (iffparsebase:= OpenLibrary('iffparse.library',0))=NIL
  51.         Raise ("iffp")
  52.     ENDIF
  53.  
  54.     -> You *MUST* use AllocIFF() to allocate an iffhandle structure
  55.     IF (iff:=AllocIFF())=NIL THEN Raise ("iffh")
  56.  
  57.     -> Prepare the iffhandle to use a dos filehandle
  58.     InitIFFasDOS(iff)
  59.  
  60.     -> Open a file and fill inn the iffhandle
  61.     IF (fh:=Open (name,OLDFILE) )=0 THEN Raise ("open")
  62.     iff.stream:=fh
  63.  
  64.     -> Start a new IO session
  65.     IF OpenIFF (iff,IFFF_READ) THEN Raise ("oiff")
  66.  
  67.  
  68. -> --- MAIN LOOP
  69.  
  70.     nest:=0;count:=0
  71.  
  72.     WHILE (ret:=ParseIFF(iff,IFFPARSE_STEP))<>IFFERR_EOF
  73.         CtrlC()
  74.  
  75.         INC count
  76.         IF ret=IFFERR_EOC
  77.             DEC nest
  78.             Write (stdout,{spaces},nest*2)
  79.             Flush(stdout)
  80.             IF count>1 THEN PutStr ('\n')
  81.             PutStr ('}')
  82.         ELSE
  83.             count:=0
  84.             PutStr ('\n')
  85.             node:= CurrentChunk(iff)
  86.             Write (stdout,{spaces},nest*2)
  87.             Flush(stdout)
  88.             Vprintf ('"%s" / "%s", size = %ld {',
  89.                 [[node.id,0],[node.type,0],node.size])
  90.             INC nest
  91.         ENDIF
  92.     ENDWHILE
  93.  
  94.     PutStr ('\n\n')
  95. EXCEPT DO
  96.  
  97. ->--- CLEANUP:
  98.  
  99.     -> Was iffparse.library opened?
  100.     IF iffparsebase
  101.  
  102.         -> Was the iffhandle structure allocated?
  103.         IF iff
  104.  
  105.             -> Did OpenIFF() fail?
  106.             IF exception<>"oiff" THEN CloseIFF(iff)
  107.  
  108.             -> Was the file opened?
  109.             IF fh THEN Close(fh)
  110.  
  111.             -> Free the iffhandle. *MUST* be done with FreeIFF()
  112.             FreeIFF(iff)
  113.         ENDIF
  114.         CloseLibrary (iffparsebase)
  115.     ENDIF
  116.  
  117.     -> IF an exception occured, let the next exception handler deal
  118.     -> with it too...
  119.  
  120.     ReThrow()
  121. ENDPROC
  122.  
  123.  
  124. PROC main() HANDLE
  125.     DEF rdargs,args
  126.  
  127.     args:=[0]
  128.     IF rdargs:=ReadArgs ('FILENAME/A',args,NIL)
  129.         readiff (args[0])
  130.     ENDIF
  131. EXCEPT
  132.     IF exception = "^C"
  133.         PutStr ('***BREAK\n')
  134.     ELSE
  135.         Vprintf ('exception = %ld ("%s")\n',[exception,[exception,0]])
  136.     ENDIF
  137. ENDPROC
  138.  
  139. spaces: CHAR '                                                  '
  140.